import { getProjectPermission,findByCity ,getFirst,addFun,queryInfo} from './policyEditServ.js'
// import { parse } from 'qs'
import { message } from 'antd'
// import moment from 'moment'
import { makeAddress } from "utils/util"
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
import { hashHistory } from 'dva/router'
import $ from 'jquery';

const defultState = {
    actionType: 'add',
    btnLoading: false,
    biz: {
        queryForm: {}
    },
    areaList: [],
    cityList: [],
    attributeList: [],
    templist:[],
    areaName: [],
    upObj:{
        areaId:'',  
        areaName:'',  
        cityId:'',  
        cityName: '',
        policyPublishTime: '',
        marketSignal:''
    },
    query: {
        id: '',
        attributeItem:{
          tableName: '政策盘点',
          itemValue: ''
        },
      },
}

const tmpModule = {
    state: cloneDeep(defultState),
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                if ('' + tmpModule.pathname === '' + pathname) {
                      // 执行业务操作
                    dispatch({ type: 'updateStore', payload: { ...cloneDeep(defultState), ...query } })//每次进去页面先重置状态
                    dispatch({type: 'queryInfo',payload: {id:query.id}})
                    dispatch({ type: 'getAreaList', payload: {} });
                }
            })
        },
    },
    effects: {
        *queryInfo({payload},{put,select,call}){
            let {attributeList,templist,actionType,query,areaName,upObj} = yield select(state => state[tmpModule.namespace])
            if(actionType == 'add'){
                let params = {
                    type: '政策盘点'
                }
                let { success, msg, attribute } = yield call(getFirst, {...params})
                templist = attribute
                for(var i=0;i< 7; i++){
                    let obj={
                        list :[],
                        name :''
                    }
                    attributeList.push(obj)
                }
                for(var item of attribute){
                    
                    if(item.tableTab == '限购政策'){
                        attributeList[0]['list'].push(item)
                        attributeList[0]['name'] = '限购政策'
                    }
                    if(item.tableTab == '限售政策'){
                        attributeList[1]['list'].push(item)
                        attributeList[1]['name'] = '限售政策'
                    }
                    if(item.tableTab == '限价政策'){
                        attributeList[2]['list'].push(item)
                        attributeList[2]['name'] = '限价政策'
                    }
                    if(item.tableTab == '限签政策'){
                        attributeList[3]['list'].push(item)
                        attributeList[3]['name'] = '限签政策'
                    }
                    if(item.tableTab == '限贷政策'){
                        attributeList[4]['list'].push(item)
                        attributeList[4]['name'] = '限贷政策'
                    }
                    if(item.tableTab == '其他政策'){
                        attributeList[5]['list'].push(item)
                        attributeList[5]['name'] = '其他政策'
                    }
                    if(item.tableTab == '异动政策'){
                        attributeList[6]['list'].push(item)
                        attributeList[6]['name'] = '异动政策'
                    }
                }
                yield put({type: 'updateStore', payload: {attributeList,templist}})
            }
            if(actionType == 'edit'||actionType == 'cook'){
                let params = { 
                    id: payload.id,
                  }
                let { success, msg, result } = yield call(queryInfo, {...params})
                templist =  JSON.parse(result.attributeItem.itemValue)
                query = result
                upObj.areaId =  result.areaId
                upObj.areaName =  result.areaName 
                upObj.cityId =  result.cityId
                upObj.cityName= result.cityName
                upObj.policyPublishTime= result.policyPublishTime
                upObj.marketSignal= result.marketSignal
                for(var i=0;i< 7; i++){
                    let obj={
                        list :[],
                        name :''
                    }
                    attributeList.push(obj)
                }
                for(var item of templist){
                    if(item.attributeName == '行政区域'){
                        areaName = JSON.parse(item.defaultValue)
                    }
                    if(item.tableTab == '限购政策'){
                        attributeList[0]['list'].push(item)
                        attributeList[0]['name'] = '限购政策'
                    }
                    if(item.tableTab == '限售政策'){
                        attributeList[1]['list'].push(item)
                        attributeList[1]['name'] = '限售政策'
                    }
                    if(item.tableTab == '限价政策'){
                        attributeList[2]['list'].push(item)
                        attributeList[2]['name'] = '限价政策'
                    }
                    if(item.tableTab == '限签政策'){
                        attributeList[3]['list'].push(item)
                        attributeList[3]['name'] = '限签政策'
                    }
                    if(item.tableTab == '限贷政策'){
                        attributeList[4]['list'].push(item)
                        attributeList[4]['name'] = '限贷政策'
                    }
                    if(item.tableTab == '其他政策'){
                        attributeList[5]['list'].push(item)
                        attributeList[5]['name'] = '其他政策'
                    }
                    if(item.tableTab == '异动政策'){
                        attributeList[6]['list'].push(item)
                        attributeList[6]['name'] = '异动政策'
                    }
                }
                yield put({type: 'updateStore', payload: {query,attributeList,templist,areaName,upObj}})
                yield put({type: 'findByCity',payload: {areaId:query.areaId}})
            }
        },
        *getAreaList({payload}, {put, select, call}){
            let {areaList} = yield select(state => state[tmpModule.namespace])
            let params = { 
              userId: localStorage.username
            }
            const { success,projectList} = yield call(getProjectPermission, {...params})
            areaList =  projectList.map((item,index)=>{
                          return {
                            label: item.areaName,
                            value: item.areaId,
                          }
                        })
              yield put({
                type: 'updateStore',
                payload: {areaList}
              })
        },
        *findByCity({payload}, {put, select, call}){
            let {cityList} = yield select(state => state[tmpModule.namespace])
            let params = { 
              area: payload.areaId,
            }
            const { success,result} = yield call(findByCity, {...params})
            if(success){
              cityList = result.filter(item => !!item)
              yield put({
                type: 'updateStore',
                payload: {cityList}
              })
            }
          },
        *submit({payload},{put,select,call}){
            let {btnLoading,query,attributeList,areaName,templist,actionType,upObj,areaList,cityList} = yield select(state => state[tmpModule.namespace])
            let arr = []
            let arr1 = []
            for(var yyp of areaList){
                if(upObj.areaId == yyp.value){
                    upObj.areaName = yyp.label
                }
            }
            for(var ppt of cityList){
                if(upObj.cityId == ppt.cityid){
                    upObj.cityName = ppt.city
                }
            }
            for(var iten of attributeList){
                for(var itt of iten.list){
                    arr.push(itt)
                }
            }
            for(var ipp of templist){
                if(ipp.attributeName == '区域'){
                    ipp.defaultValue = upObj.areaName 
                    arr1.push(ipp)
                }else if(ipp.attributeName == '城市'){
                    ipp.defaultValue = upObj.cityName  
                    arr1.push(ipp)
                }if(ipp.attributeName == '市场信号'){
                    ipp.defaultValue = upObj.marketSignal
                    arr1.push(ipp)
                }else if(ipp.attributeName == "政策发布时间"){
                    ipp.defaultValue = upObj.policyPublishTime
                    arr1.push(ipp)
                }
            }
            arr=arr1.concat(arr)
            let list =  arr.filter(item => !!item.tableField)
            for(var i = 0; i < list.length; i++){
                query[list[i].tableField] = list[i].defaultValue
            }
            query.attributeItem.itemValue = JSON.stringify(arr)
            if(actionType == 'cook'){
                delete query.id
                delete query.attributeItem.id
            }
            query.areaId =  upObj.areaId 
            query.areaName =  upObj.areaName 
            query.cityId =  upObj.cityId 
            query.cityName =  upObj.cityName 
            query.policyPublishTime =  upObj.policyPublishTime 
            query.createBy = localStorage.nickname
            query.createUser = localStorage.username

            let params = query 
            btnLoading = true
            yield put({type: 'updateStore', payload: {btnLoading}})
            let { success, msg, data } = yield call(addFun, {...params})
            let flag = success.toString()
            if(flag){
              btnLoading = false
              yield put({type: 'updateStore', payload: {btnLoading}})
            }
            if(success){
              message.success('提交成功')
              hashHistory.push({
                pathname: '/home/predictManage/policyList',
                query: {}
              })
            }
          },
    },
    // 存入状态机
    reducers: {
        updateStore(preState, action) {
            return Object.assign({}, preState, action.payload)
        },
    }
}

export default tmpModule
